<?php
    $title = 'Nouveau site';
    $this->headTitle($title);
    
    $dWk = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche') ;
?>    


<div class="content-head">
    <?php $name = ''; ?>
    <h3><?php echo $title; ?></h3>
    <div class="toolbar">
        <button class='btn btn-inverse' onclick="location.href = '<?php echo $this->url('RHX/default', array('controller'	 => 'site')) ?>';">Annuler et revenir à la liste des sites </button>
    </div>
</div>

<div class="content-body clear-auto">
    <div class='site-top '>        
        <div style="margin-top: 20px;" class="inf-site inf-site-violet"> 
        Libellé : <a href='#' class='' id= 'site-libelle' href='#'  data-type="text" >  </a>
        </div>
    </div>
    <div class='site-left '>
       <div class="cnt-photo-site" >
            <a href='javascript: void(0);' class='cnt-img-site ' id='link-uploadPhoto'>
                <?php 
                
                //$basePath	 = str_replace('public', '', $this->basePath()); 
                $basePath	 =  $this->basePath(); 
                $photoUrl = $basePath . '/img/rhx/site-default.jpg'; 
                ?>

            <img id="site-photo" src="<?php echo  $photoUrl ;?>" width="320" height="auto">
            </a>
            <a href="#" id="btn-uploadPhotoSite" data-id=""  class='btn btn-info btn-small' style="display:none; z-index:55555;">
		    Télécharger photo
		</a>
       </div>   
       <div class='inf-site inf-site-violet' style='margin-top: 53px;'> Siret : 
           <a href='#' class='' id= 'site-siret' href='#'  data-type="text" >  </a></div>
       <div class='inf-site inf-site-violet'> TVA : 
           <a href='#' class='' id= 'site-tva' href='#'  data-type="text" > </a></div>
       <div class='inf-site inf-site-violet'> N° CRT : 
           <a href='#' class='' id= 'site-crt' href='#' data-type="text"> </a></div>
       <div class='inf-site inf-site-violet'> N° Police Assurance : 
           <a href='#'  class='' id= 'site-assurance' href='#' data-type="text"> </a></div>       
       <div class='inf-site inf-site-violet'>  N°  d’adhérent Urssaf : 
           <a href='#'  class='' id= 'site-urssaf' href='#' data-name="urssaf" data-type="text"> </a></div>       
           
       <div class='inf-site inf-site-beige' style='margin-top: 20px;'> Médecine du travail N° Adhérent : 
           <a   href='#'  id= 'site-medecine_travail' data-type="text"> </a></div>
       <div class='inf-site inf-site-beige'> Centre de Visite :
           <a href='' id= 'site-centre_visite'  data-type="text" >  </a></div>   
       
   </div>
    
   <div class='site-right'>
       <div  class='inf-site inf-site-bleu' style='margin-top: 20px;' > Adresse : 
           <a id= 'site-adresse' href='#'class='' data-type="textarea"> </a></div>
       
       <div class='inf-site inf-site-bleu' > Téléphone : 
           <a id= 'site-phone' href='#' class=''   data-type="text" ></a></div>     
       <div class='inf-site inf-site-bleu' style=''> Fax : 
           <a id= 'site-fax'  href="#" id= '' class=''   data-type="text" ></a></div>
           
       <div class='inf-site inf-site-bleu'> Mail :
           <a href='#' id= 'site-mail' class=''   data-type="text" >  </a></div>     
       <div class='inf-site inf-site-bleu' style=''  > Métro : 
           <a href='' id= 'site-metro'  data-type="text" class=''></a></div>       
       
       <div class='cnt-inf-site cnt-inf-site-dir clear-auto' style='margin-top: 20px;'>
            <?php 
                $dir = isset($this->directeur) ? $this->directeur : null;
                $infDir = $dir? $dir->infDir : '';
            ?>
            <!--div class='inf-site inf-site-pink ' > Directeur : 
                <a  href='#' id= 'site-directeur' class=''  data-type="select"> <?php //echo $infDir ; ?> </a></div-->     
            <div class='inf-site inf-site-pink ' > Marché N° : 
                <a href='' id= 'site-marche'  data-type="text"> </a></div>       
       </div>
       
       <div class='cnt-inf-site cnt-inf-site-rib_top clear-auto' style='margin-top: 20px;'>            
            <div class='inf-site inf-site-yellow ' > RIB :   
		
		<span id='label-uploadRib'></span>
		
		<a href='javascript: void(0);' class='btn btn-info btn-small' id= 'link-uploadRib' >Télécharger</a>
	    </div>     
		
            
       </div>
       <!-- horaire -->
            <div class='cnt-inf-site clear-auto' style='margin-top: 20px;'>                     
                <div class='inf-site inf-site-white inf-site-border-blue' style='margin-bottom: 6px;'> 
                         Horaire du site 
                </div>
                <table id='table-site-horaire' class='table table-striped '>
                 <thead>
                  <tr>
                      <?php foreach ($dWk as $d ): ?>
                      <td>
                          <?php echo strtoupper($d[0]);?>
                          <!--input class="checkbox checkbox-open-state" type="checkbox" value='' <?php echo $horaire->$d ? 'checked' : '' ;?> data-pk='<?php echo $horaire->id;?>' data-name='<?php echo $d; ?>'-->
                      </td>
                      <?php endforeach ;?>


                  </tr>
                 </thead>

                  <tbody>
                      <tr>
                         <?php foreach ($dWk as $d ): ?>
                         <td style=' padding-top: 0 ;padding-bottom: 0 ;'>

                             <input class="checkbox checkbox-open-state" type="checkbox" value='' <?php echo $horaire->$d ? 'checked' : '' ;?> data-pk='<?php echo $horaire->id;?>' data-name='<?php echo $d; ?>'>
                         </td>
                         <?php endforeach ;?>


                     </tr>
                      <tr> 
                          <?php foreach ($dWk as $d ): ?>
                             <?php $d_ouv = $d. '_ouverture' ;?>

                             <td><a class="hr-ouverture hr-ouverture-<?php echo $d;?>" data-title="Heure d'ouverture le <?php echo $d;?>" data-pk="<?php echo $horaire->id?>" data-name='<?php echo $d_ouv; ?>' data-viewformat="HH\h mm\m" data-format="HH:mm" data-template="HH:mm" data-type="combodate" href="#" > 
                                     <?php if ($horaire->$d):                                 
                                             $tm = strtotime($horaire->$d_ouv);
                                             $s = sprintf ("%sh %sm", date("H", $tm), date("i", $tm) );

                                           else : 
                                             $s = '&nbsp;' ;
                                           endif ;
                                           echo $s;
                                      ?>
                                 </a>
                             </td>

                          <?php endforeach ;?>
                      </tr>
                      <tr> 
                           <?php foreach ($dWk as $d ): ?>
                             <?php $d_fer = $d. '_fermeture' ;?>
                             <td><a class="hr-fermeture hr-fermeture-<?php echo $d;?>" data-title="Heure de fermeture le <?php echo $d;?>" data-pk="<?php echo $horaire->id?>" data-name='<?php echo $d_fer; ?>' data-viewformat="HH\h mm\m" data-format="HH:mm" data-template="HH:mm" data-type="combodate" href="#"> 
                                     <?php if ($horaire->$d):                                 
                                             $tm = strtotime($horaire->$d_fer);
                                             $s = sprintf ("%sh %sm", date("H", $tm), date("i", $tm) );

                                           else : 
                                             $s = '&nbsp;' ;
                                           endif ;
                                           echo $s;
                                      ?>
                                 </a>
                             </td>
                          <?php endforeach ;?>
                      </tr>
                       
                  </tbody>      
                 </table>




            <!--div  class='cnt-inf-site cnt-inf-site-rib' >           
                <a href='javascript: void(0);' class='bt bt-show'>Affichage obligatoire</a>                
            </div-->

        </div>
       
       
   </div>
    <form id="newSite" action="<?php echo $this->url('RHX/default', array('controller'	 => 'site', 'action'	 => 'addNewEx')) ?>" method="post" >
        
        <input type="hidden" value="-1" name="id" id="data-site-id">
        
        <?php $aInfSite = array('libelle', 'adresse', 'phone',  'fax', 'mail', 'metro', 'siret', 'tva', 'crt', 'assurance','urssaf', 'medecine_travail', 'centre_visite', 'marche', 'photo', 'rib', 'photo_extension', 'rib_extension')?>
        <?php foreach ($aInfSite as $k=>$v) :?>
            <input type="hidden" id="data-site-<?php echo $v;?>" name="<?php echo $v;?>" value="" >    
        <?php endforeach ;?>
            
        <?php $dWk = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche')?>
        <?php foreach ($dWk as $d) :?>
            <input type="hidden" id="data-site-<?php echo $d;?>" name="horaire[<?php echo $d;?>]" value="<?php echo $horaire->$d? 1 : 0;?>" >    
            <input type="hidden" id="data-site-<?php echo $d.'_ouverture';?>" name="horaire[<?php echo $d.'_ouverture';?>]" value="<?php $d_oc = $d.'_ouverture'; echo $horaire->$d_oc;?>" >    
            <input type="hidden" id="data-site-<?php echo $d.'_fermeture';?>" name="horaire[<?php echo $d.'_fermeture';?>]" value="<?php $d_oc = $d.'_fermeture'; echo $horaire->$d_oc;?>" >    
        <?php endforeach ;?>
    </form>
</div>
<div class="content-head">
    <h3>&nbsp;</h3>
    <div class="toolbar">
	<button  id='bt-addNewSite' class="btn btn-success">Créer le Nouveau Site</button>
        <button class='btn btn-inverse' onclick="location.href = '<?php echo $this->url('RHX/default', array('controller'	 => 'site')) ?>';">Annuler et revenir à la liste des sites </button>	
    </div>
</div>
<script type='text/javascript'>
    
    $(function(){
        
        var site_inf =  {'libelle' : 'Libellé du site','adresse' : 'Adresse', 'phone': 'Téléphone', 'fax':'Fax', 
                          'metro' : 'Metro', 'siret' : 'Siret', 'tva': 'TVA' ,
                          'crt' : 'N° CRT', 'assurance': "N° Police d'assurance",
                          'urssaf': "N° d'adhérent Urssaf",
                          'medecine_travail':"Médecine du travail N° Adhérent", 
                          'centre_visite': "Centre de Visite", 'marche' :  "N° Marché" }; 
            
        $.each(site_inf, function(k, v){
            //console.log(k + ' > ' +v);
            
            id = 'site-' + k;
            $('#' + id).editable({
                title: v,
                validate: function(value){
                    $('#data-site-' + k).val(value);
                    console.log( $('#data-site-' + k).val() );
                }
		});            
        });
      
        $('#site-mail').editable({
                title: 'Adresse électronique',                
                validate : function(v){
                    regExpMail =  new RegExp(/^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/);

                    if(!regExpMail.test(v))
                        return 'Adresse mail invalide';
                }
                    });
            
        // toggle bouton upload photo
        $('.cnt-photo-site').hover(function(e){                    
                     $('#btn-uploadPhotoSite').css('display', '');
                }).mouseleave(function(e){                    
                     $('#btn-uploadPhotoSite').css('display', 'none');
                });
                
        // uploading photo        
        $(['#link-uploadPhoto', '#btn-uploadPhotoSite']).each(function (k, id){
            $(id).on ('click', function(e){
               e.preventDefault();
               uploadWindow = window.open('<?php echo $this->url('RHX/default', array('controller'	 => 'site', 'action'	 => 'uploadPhoto')) ?>', 'UploadWindow', 'menubar=no, scrollbars=no, top=100, left=100, width=500, height=400');
            });
        });
        
         // uploading rib
        $('#link-uploadRib').on('click', function(e){
               e.preventDefault();
               uploadWindow = window.open('<?php echo $this->url('RHX/default', array('controller'	 => 'site', 'action'	 => 'uploadRib')) ?>', 'UploadWindow', 'menubar=no, scrollbars=no, top=100, left=100, width=500, height=400');
            });
        
        //ajout site    
        $('#bt-addNewSite').on('click', function(e){
            var libelle = $('#data-site-libelle').val();
            
            // reset errorClass
            $('.errorSaisie-site').removeClass('errorSaisie-site');
            
            err = false;
            errMsg = new Array();
            errCount = 0;
            
            //validation
            if(libelle.length < 2){
                err = true;
                errMsg[errCount++] = "Le libellé est vide ou trop court";
                $('#site-libelle').parent().addClass('errorSaisie-site'); 
            }
            
            if( err){
                msg = "<ol>";
                for (i = 0; i < errCount; i++) {
                    msg += '<li>' + errMsg[i] + "</li>";
                }
                msg += '</ol>';
                showError(msg); 
                
                return false;
            }
                
            $('form#newSite').submit();
        });
        // horaire du site
        
       $(['.hr-fermeture', '.hr-ouverture']).each(function(k,v){
           $(v).each(function(){
                $(this).editable({
                     /*placement: 'right',*/
                     combodate: {
                         firstItem: 'name',
                         minuteStep: 5
                     },                
                     validate: function(val){
                         if(!val)
                             return 'heure invalide!';

                         $this = $(this);
                         hr_mn = val.hour()+ ':'+ val.minute();

                         name = $this.data('name');
                         $inpHid = $("#data-site-" +name);
                         $inpHid.val(hr_mn);    
                         
                         console.log("#data-site-" +name + ' > '+ hr_mn)
                     },
                     success: function(val){
                         console.log('success');
                     }
                 });           
       });
       });
       
       $('.checkbox-open-state').each(function(){
           var $this = $(this),
               init = function(){
                   var  day = $this.data('name'),
                        $hrOuv = $('.hr-ouverture-'+ day ),
                        $hrFer = $('.hr-fermeture-'+ day );

                   isChecked = $this.is(':checked') ? 1 : 0;
                   
                   $hrOuv.editable(isChecked ? 'enable' :'disable');
                   $hrFer.editable(isChecked ? 'enable' :'disable');
                   if(!isChecked){ 
                       $hrOuv.html('...'); 
                       $hrFer.html('...');
                   }
                   // design bootstrap
                   $this.checkbox();
               };
           //init 
           init();
           
           //on change
           $this.on( 'change', function(){
               var $this = $(this),
                   day = $this.data('name'),
                   $hrOuv = $('.hr-ouverture-'+ day ),
                   $hrFer = $('.hr-fermeture-'+ day );;
               
               isChecked = $this.is(':checked') ? 1 : 0;
               
               $inpHid = $("#data-site-" +day);
               $inpHid.val(isChecked);  
               
               // enable/disable
                    $hrOuv.editable(isChecked ? 'enable' :'disable');
                    $hrFer.editable(isChecked ? 'enable' :'disable');
                    
               //update
               if(isChecked){
                        $hrOuv.text('07h 00m').removeClass('editable-empty');
                        $hrFer.text('23h 00m').removeClass('editable-empty');
                        $('#data-site-'+ day +'_ouverture').val('07:00');
                        $('#data-site-'+ day +'_fermeture').val('23:00');
                    }
                    else{
                        $hrOuv.text('...');
                        $hrFer.text('...');
                        
                        $('#data-site-'+ day +'_ouverture').val('0');
                        $('#data-site-'+ day +'_fermeture').val('0');
                    }
            });           
       });
       
    });
    
</script>